import wxApi from "../../utils/wxApi"
import request from "../../utils/request"
import $router from "../../utils/router"
import store from "../../store"

const utils = require("../../utils/util")
// const IM = require("../../utils/im/im")
module.exports = {
  getWxCode() {
    return new Promise((resolve, reject) => {
      wxApi("login").then(res => {
        if (res.errMsg.indexOf("ok") !== -1) {
          store.data.loginWxCodeNum = res.code
          resolve(res.code)
        } else {
          utils.message({
            content: "wx.login 返回结果出错",
            type: "none"
          })
          reject(null)
        }
      }).catch(() => {
        utils.message({
          content: "wx.login 调用方法出错",
          type: "none"
        })
        reject(null)
      })
    })
  },
  getUserByWxCode(code) {
    return new Promise((resolve, reject) => {
      request.post("WX_CODE_LOGIN", { data: { code } }).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  wxGetUserInfoApi() {
    return new Promise((resolve, reject) => {
      wxApi("getUserInfo", { withCredentials: "true" }).then(res => {
        if (res.errMsg.indexOf("ok") !== -1) {
          resolve(res)
        } else {
          reject(res)
        }
      }).catch(err => {
        reject(err)
      })
    })
  },
  loginSuccess(data) {
    /**
     * 1. 登录成功需要做的事情
     * 2. 可能需要拉取自己的用户信息，个人资料啥的
     * */

    if (data && utils.isObj(data) && data.token) {
      request.setToken(data.token)
      store.data.needLogin = false
      store.data.loginInfo = data
      store.isLogin()
      return store.update()
    } else {
      utils.message({
        content: "token 不能为空",
        type: "error"
      })
    }
  },
  wxLoginByWxUserInfo(data = {}) {
    const { code,encryptedData, iv } = data
    return new Promise((resolve, reject) => {
      request.python.post("PY_LOGIN_BY_UNIOND_ID", {
        data: {
          code,
          user_info: {
            encrypted_data: encryptedData,
            iv: iv
          }
        }
      },"NO_STRUCTURE").then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  toPhonePage(query = {}, type = "navigateTo") {
    $router[type]("PHONE_LOGIN_PAGES", query)
  }
}
